package cgl.narada.gui.admin.reliable.viewtable;

import javax.swing.JLabel;

import cgl.narada.event.TemplateInfo;
import cgl.narada.gui.admin.reliable.UserConfigure;
import cgl.narada.service.storage.StorageService;
import cgl.narada.service.storage.StorageServiceFactory;
import cgl.narada.service.storage.db.DataAccessObject;
import cgl.narada.service.storage.db.DatabaseFactory;
import cgl.narada.service.storage.db.DatabaseStatements;

public class TimestampInfoNbTableModel extends NbTableModel {
	private DatabaseStatements	statements				= DatabaseStatements.getInstance();
	private boolean initialized = false;

	public TimestampInfoNbTableModel() {

    columnData = new ColumnData[3];
    columnData[0] = new ColumnData("Sequence Number", 200, JLabel.LEFT);
    columnData[1] = new ColumnData("Timestamp", 200, JLabel.LEFT);
    columnData[2] = new ColumnData("Template ID", 200, JLabel.LEFT);
  }

  public Object getValueAt(int rowNum, int columnNum) {
    if (rowNum < 0 || rowNum >= getRowCount()) {
      return "";
    }
    TimestampRawData row = (TimestampRawData) allRow.elementAt(rowNum);
    switch (columnNum) {
      case 0:
        return row.sequenceNumber;
      case 1:
        return row.timestamp;
      case 2:
        return row.templateId;
    }
    return "";
  }

  /*
   public void setDefaultData(){
   allRow.removeAllElements();
   allRow.addElement(new InventoryRowData("12345","12344", "66"));
   }
   */

  public void setDefaultData() {
    allRow.removeAllElements();
    System.out.println("$$$$$$$ impl=" + impl);
    if (impl.equals("db")) {
      try {
        // DataAccessObject dao = new DataAccessObject();
        DataAccessObject dao = null;

        DatabaseFactory df = new DatabaseFactory();
        try {
          dao = df.getDAO();
					statements = DatabaseStatements.getInstance();
					statements.initialize(dao.getConnection(), dao);
				} catch (Exception ex) {
          System.out.println("exception in creating DataAccessObject ");
        }

				StorageService storageService = StorageServiceFactory
						.getStorageService("db", UserConfigure.getProperties());
				TemplateInfo[] templateInfos = storageService
						.getListOfManagedTemplates();
				for (int i = 0; i < templateInfos.length; i++) {
					TemplateInfo oneTemplate = templateInfos[i];
//					String query = " select sequenceNumber, timestamp, templateId from TimestampInfo "
//							+ "where templateId="
//							+ oneTemplate.getTemplateId()
//							+ " limit 0,100;";
//		       java.sql.ResultSet rs = dao.executePreparedQuery(query);
					java.sql.ResultSet rs = statements
							.executeTimestampInfoCompleteRowStmt(""
									+ oneTemplate.getTemplateId());
		        while (rs.next()) {
		          allRow.add(new TimestampRawData(rs.getString(1), rs.getString(2), rs
		              .getString(3)));
		        }
		        
//						String query2 = " select sequenceNumber, timestamp, templateId from TimestampInfo "
//							+ "where templateId="
//							+ oneTemplate.getTemplateId()
//							+ " order by sequenceNumber desc limit 0,100;";
//		       java.sql.ResultSet rs2 = dao.executePreparedQuery(query2);
						java.sql.ResultSet rs2 = statements
						.executeTimestampInfoCompleteRowDESCStmt(""
								+ oneTemplate.getTemplateId());
		        while (rs2.next()) {
		          allRow.add(new TimestampRawData(rs2.getString(1), rs2.getString(2), rs2
		              .getString(3)));
		        }
		        
				}
		
//        String query = " select sequenceNumber, timestamp, templateId from TimestampInfo;";
//        java.sql.ResultSet rs = dao.executePreparedQuery(query);
//        while (rs.next()) {
//          allRow.add(new TimestampRawData(rs.getString(1), rs.getString(2), rs
//              .getString(3)));
//        }
      } catch (Exception e) {
        e.printStackTrace();
        System.out.println("exception happen during create DataAccessObject");
      }
    } else if (impl.equals("file")) {
      new FileStorageGuiHelp().fetchInventory(allRow);
    }
  }

}